package com.gaohe.dcp.service.impl;

import com.gaohe.dcp.domain.dto.ResDTO;
import com.gaohe.dcp.domain.dto.result.Result;
import com.gaohe.dcp.service.dcpTestService;
import com.gaohe.dcp.domain.vo.ResVO;
import com.gaohe.dcp.domain.vo.ThirdVO;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

@Service
public class dcpTestServiceImpl implements dcpTestService {
    private static final String filePath = "D:\\结果.xlsx";

    @Override
    public Result exec(ResDTO resDTO) throws IOException {
        ResDTO.FirstDTO first = resDTO.getFirst();
        int age = first.getAge();
        int gender = first.getGender();
        //第二部分
        int sum1 = 0, sum2 = 0, sum3 = 0;
        if (!(age >= 4 && age <= 5)) {
            ResDTO.SecondDTO second = resDTO.getSecond();
            //1.算分
            //1,2,3,4
            sum1 = sum1And2And3And4(second, 0);
            //3,5,6
            sum2 = sum3And5And6(second, 0);
            //7
            sum3 = sum7(second, 0);
        }
        //第三部分
        List<ThirdVO> resList = new ArrayList<>();
        ResDTO.ThirdDTO third = resDTO.getThird();
        List<Integer> numList = third.getMarks();
        //2.评估
        ResVO vo = null;
        if (gender == 1) {
            if (age >= 4 && age <= 5) {
                fun4_5_1(numList, resList);
                System.out.println(resList);
                output3(first, resList);
                //参数赋值
                vo = new ResVO(first, resList);
            } else if (age >= 6 && age <= 11) {
                //第二部分
                String desc1 = "正常", desc2 = "正常", desc3 = "正常";
                if (sum1 < 3) desc1 = "异常";
                if (sum2 < 3) desc2 = "异常";
                if (sum3 < 2) desc3 = "异常";

                //第三部分
                fun6_11_1(numList, resList);
                output2_3(first, sum1, desc1, sum2, desc2, sum3, desc3, resList);
                //参数赋值
                vo = new ResVO(first, sum1, desc1, sum2, desc2, sum3, desc3, resList);
                System.out.println(resList);
            } else if (age >= 12 && age <= 16) {
                String desc1 = "正常", desc2 = "正常", desc3 = "正常";
                if (sum1 < 3.5) desc1 = "异常";
                if (sum2 < 3.5) desc2 = "异常";
                if (sum3 < 2) desc3 = "异常";

                fun12_16_1(numList, resList);
                output2_3(first, sum1, desc1, sum2, desc2, sum3, desc3, resList);
                //参数赋值
                vo = new ResVO(first, sum1, desc1, sum2, desc2, sum3, desc3, resList);
                System.out.println(resList);
            } else {
                return Result.fail("年龄不在测评范围内，请重新输入");
            }
        } else if (gender == 2) {
            if (age >= 4 && age <= 5) {
                fun4_5_2(numList, resList);
                System.out.println(resList);
                output3(first, resList);
                //参数赋值
                vo = new ResVO(first, resList);
            } else if (age >= 6 && age <= 11) {
                String desc1 = "正常", desc2 = "正常", desc3 = "正常";
                if (sum1 < 2.5) desc1 = "异常";
                if (sum2 < 3.5) desc2 = "异常";
                if (sum3 < 3) desc3 = "异常";

                fun6_11_2(numList, resList);
                output2_3(first, sum1, desc1, sum2, desc2, sum3, desc3, resList);
                //参数赋值
                vo = new ResVO(first, sum1, desc1, sum2, desc2, sum3, desc3, resList);
                System.out.println(resList);
            } else if (age >= 12 && age <= 16) {
                String desc1 = "正常", desc2 = "正常", desc3 = "正常";
                if (sum1 < 3) desc1 = "异常";
                if (sum2 < 3) desc2 = "异常";
                if (sum3 < 3) desc3 = "异常";

                fun12_16_2(numList, resList);
                output2_3(first, sum1, desc1, sum2, desc2, sum3, desc3, resList);
                //参数赋值
                vo = new ResVO(first, sum1, desc1, sum2, desc2, sum3, desc3, resList);
                System.out.println(resList);
            } else {
                return Result.fail("年龄不在测评范围内，请重新输入");
            }
        } else {
            return Result.fail("请输入性别");
        }

        return Result.ok(vo);
    }

    private void output2_3(ResDTO.FirstDTO first, int sum1, String desc1, int sum2, String desc2, int sum3, String desc3, List<ThirdVO> resList) throws IOException {
        Workbook workbook = initExcel();

        // 获取当前工作表的最后一行索引，并在其下一行写入新数据
        Sheet sheet = workbook.getSheet("Sheet1");
        int lastRowNum = sheet.getLastRowNum(); // 获取最后一行的索引（从0开始）
        Row row = sheet.createRow(lastRowNum + 1);
        write2(row, sum1, desc1, sum2, desc2, sum3, desc3, workbook);
        write3(row, first, resList, workbook);

        // 写入文件
        try (FileOutputStream fileOut = new FileOutputStream(filePath)) {
            workbook.write(fileOut);
        }

        // 关闭工作簿
        workbook.close();
    }

    private static void write2(Row row, int sum1, String desc1, int sum2, String desc2, int sum3, String desc3, Workbook workbook) {
        // 创建字体样式
        Font font = workbook.createFont();
        font.setFontName("宋体");
        font.setFontHeightInPoints((short) 12);

        Sheet sheet = workbook.getSheet("Sheet1");

        // 创建单元格样式
        CellStyle style = workbook.createCellStyle();
        style.setFont(font);
        style.setAlignment(HorizontalAlignment.CENTER);
        style.setVerticalAlignment(VerticalAlignment.CENTER);

        //在Sheet页中创建行，0表示第1行
        row.createCell(4).setCellValue(sum1);
        row.createCell(5).setCellValue(desc1);
        row.createCell(6).setCellValue(sum2);
        row.createCell(7).setCellValue(desc2);
        row.createCell(8).setCellValue(sum3);
        row.createCell(9).setCellValue(desc3);

    }


    private void output3(ResDTO.FirstDTO first, List<ThirdVO> resList) throws IOException {
        Workbook workbook = initExcel();

        // 获取当前工作表的最后一行索引，并在其下一行写入新数据
        Sheet sheet = workbook.getSheet("Sheet1");
        int lastRowNum = sheet.getLastRowNum(); // 获取最后一行的索引（从0开始）
        Row row = sheet.createRow(lastRowNum + 1);
        write3(row, first, resList, workbook);

        // 写入文件
        try (FileOutputStream fileOut = new FileOutputStream(filePath)) {
            workbook.write(fileOut);
        }

        // 关闭工作簿
        workbook.close();
    }

    private static void write3(Row row, ResDTO.FirstDTO first, List<ThirdVO> resList, Workbook workbook) {

        // 创建字体样式
        Font font = workbook.createFont();
        font.setFontName("宋体");
        font.setFontHeightInPoints((short) 12);

        Sheet sheet = workbook.getSheet("Sheet1");

        // 创建单元格样式
        CellStyle style = workbook.createCellStyle();
        style.setFont(font);
        style.setAlignment(HorizontalAlignment.CENTER);
        style.setVerticalAlignment(VerticalAlignment.CENTER);

        // 创建第一行
        Cell c1 = row.createCell(0);
        c1.setCellValue(first.getChildName());
        c1.setCellStyle(style);

        Cell c2 = row.createCell(1);
        c2.setCellValue(first.getPatientNumber());
        c2.setCellStyle(style);

        Cell c3 = row.createCell(2);
        c3.setCellValue(first.getAdmissionNumber());
        c3.setCellStyle(style);

        Cell c4 = row.createCell(3);
        c4.setCellValue(first.getCardNumber());
        c4.setCellStyle(style);

        //第三部分
        int size = resList.size();
        for (int i = 0; i < size; i++) {
            Cell cc1 = row.createCell(10 + i * 3);
            cc1.setCellValue(resList.get(i).getName());
            cc1.setCellStyle(style);

            Cell cc2 = row.createCell(11 + i * 3);
            cc2.setCellValue(resList.get(i).getValue());
            cc2.setCellStyle(style);

            Cell cc3 = row.createCell(12 + i * 3);
            cc3.setCellValue(resList.get(i).getRes());
            cc3.setCellStyle(style);
        }

    }

    //初始化表格
    private static Workbook initExcel() throws IOException {
        String filePath = "D:\\o1.xlsx";
        File file = new File(filePath);

        Workbook workbook;
        Sheet sheet;
        // 判断文件是否存在
        if (file.exists()) {
            // 如果文件存在，打开现有工作簿
            try (FileInputStream fis = new FileInputStream(file)) {
                workbook = new XSSFWorkbook(fis);
            }
            // 检查Sheet1是否存在
            sheet = workbook.getSheet("Sheet1");
            if (sheet != null) {
                System.out.println("Sheet1已存在，跳过模板创建。");
                return workbook; // 如果Sheet1存在，直接退出
            } else {
                // 如果Sheet1不存在，创建Sheet1
                sheet = workbook.createSheet("Sheet1");
            }
        } else {
            // 如果文件不存在，创建新工作簿和Sheet1
            workbook = new XSSFWorkbook();
            sheet = workbook.createSheet("Sheet1");
        }

        // 创建字体样式
        Font font = workbook.createFont();
        font.setFontName("宋体");
        font.setFontHeightInPoints((short) 12);

        // 创建单元格样式
        CellStyle style = workbook.createCellStyle();
        style.setFont(font);
        style.setAlignment(HorizontalAlignment.CENTER);
        style.setVerticalAlignment(VerticalAlignment.CENTER);
        style.setBorderTop(BorderStyle.THIN);
        style.setBorderBottom(BorderStyle.THIN);
        style.setBorderLeft(BorderStyle.THIN);
        style.setBorderRight(BorderStyle.THIN);

        // 创建第一行
        Row row1 = sheet.createRow(0);
        Cell cell1 = row1.createCell(0);
        cell1.setCellValue("姓名");
        cell1.setCellStyle(style);

        Cell cell2 = row1.createCell(1);
        cell2.setCellValue("门诊号");
        cell2.setCellStyle(style);

        Cell cell3 = row1.createCell(2);
        cell3.setCellValue("住院号");
        cell3.setCellStyle(style);

        Cell cell4 = row1.createCell(3);
        cell4.setCellValue("就诊卡号");
        cell4.setCellStyle(style);

        //第二部分
        Cell cell5 = row1.createCell(4);
        cell5.setCellValue("社交能力");
        cell5.setCellStyle(style);

        Cell cell6 = row1.createCell(10);
        cell6.setCellValue("行为问题");
        cell6.setCellStyle(style);

        // 创建第二行
        Row row2 = sheet.createRow(1);
        Cell cell7 = row2.createCell(4);
        cell7.setCellValue("活动能力");
        cell7.setCellStyle(style);

        Cell cell8 = row2.createCell(6);
        cell8.setCellValue("社交情况");
        cell8.setCellStyle(style);

        Cell cell9 = row2.createCell(8);
        cell9.setCellValue("学校情况");
        cell9.setCellStyle(style);

        //第三部分
        for (int i = 0; i < 9; i++) {
            Cell c1 = row2.createCell(10 + 3 * i);
            String value = "因子" + (i + 1);
            c1.setCellValue(value);
            c1.setCellStyle(style);
        }

        //第三行
        Row row3 = sheet.createRow(2);
        for (int i = 0; i < 3; i++) {
            Cell c1 = row3.createCell(4 + 2 * i);
            c1.setCellValue("得分");
            c1.setCellStyle(style);

            Cell c2 = row3.createCell(5 + 2 * i);
            c2.setCellValue("结果");
            c2.setCellStyle(style);
        }

        //第三部分-3
        for (int i = 0; i < 9; i++) {
            Cell c1 = row3.createCell(10 + i * 3);
            c1.setCellValue("因子名称");
            c1.setCellStyle(style);

            Cell c2 = row3.createCell(11 + i * 3);
            c2.setCellValue("得分");
            c2.setCellStyle(style);

            Cell c3 = row3.createCell(12 + i * 3);
            c3.setCellValue("结果");
            c3.setCellStyle(style);
        }

        // 合并单元格
        for (int i = 0; i < 4; i++) {
            sheet.addMergedRegion(new CellRangeAddress(0, 2, i, i)); // 姓名、门诊号、住院号、就诊卡号
        }

        sheet.addMergedRegion(new CellRangeAddress(0, 0, 4, 9)); // 社交能力
        sheet.addMergedRegion(new CellRangeAddress(0, 0, 10, 36)); // 行为问题

        for (int i = 0; i < 3; i++) {
            sheet.addMergedRegion(new CellRangeAddress(1, 1, 4 + 2 * i, 5 + 2 * i)); // 社交能力-123
        }

        for (int i = 0; i < 9; i++) {
            sheet.addMergedRegion(new CellRangeAddress(1, 1, 10 + 3 * i, 12 + 3 * i)); // 行为因子-1-9
        }

        return workbook;
    }

    private void fun12_16_2(List<Integer> nums, List<ThirdVO> resList) {
        int num1 = IntStream.of(9 - 1, 12 - 1, 14 - 1, 27 - 1, 29 - 1, 30 - 1, 31 - 1, 32 - 1, 33 - 1, 34 - 1, 35 - 1, 45 - 1, 47 - 1, 50 - 1, 52 - 1, 71 - 1, 76 - 1, 100 - 1, 112 + 8 - 1).map(nums::get).sum();
        int num2 = IntStream.of(30 - 1, 51 - 1, 56 - 1 + 1, 56 - 1 + 2, 56 - 1 + 3, 56 - 1 + 4, 56 - 1 + 5, 56 - 1 + 6, 56 - 1 + 7).map(nums::get).sum();
        int num3 = IntStream.of(17 - 1, 29 - 1, 40 - 1, 47 - 1, 70 - 1 + 8, 80 - 1 + 8, 84 - 1 + 8, 85 - 1 + 8, 96 + 8 - 1).map(nums::get).sum();
        int num4 = IntStream.of(42 - 1, 54 - 1, 65 - 1 + 8, 69 - 1 + 8, 71 - 1 + 8, 75 - 1 + 8, 77 - 1 + 8, 80 - 1 + 8, 86 - 1 + 8, 88 - 1 + 8, 102 - 1 + 8, 103 - 1 + 8, 111 + 8 - 1).map(nums::get).sum();
        int num5 = IntStream.of(0, 8 - 1, 10 - 1, 11 - 1, 13 - 1, 17 - 1, 25 - 1, 38 - 1, 48 - 1, 58 - 1 + 8, 62 - 1 + 8, 64 - 1 + 8, 80 - 1 + 8, 83 - 1 + 8, 98 + 8 - 1).map(nums::get).sum();
        int num6 = IntStream.of(8 - 1, 22 - 1, 23 - 1, 26 - 1, 39 - 1, 41 - 1, 43 - 1, 61 - 1 + 8, 63 - 1 + 8, 67 - 1 + 8, 69 - 1 + 8, 81 - 1 + 8, 82 - 1 + 8, 90 - 1 + 8, 101 - 1 + 8, 105 + 8 - 1).map(nums::get).sum();
        int num7 = IntStream.of(3 - 1, 7 - 1, 16 - 1, 19 - 1, 22 - 1, 27 - 1, 33 - 1, 34 - 1, 37 - 1, 57 - 1 + 8, 68 - 1 + 8, 74 - 1 + 8, 86 - 1 + 8, 87 - 1 + 8, 88 - 1 + 8, 89 - 1 + 8, 90 - 1 + 8, 93 - 1 + 8, 94 - 1 + 8, 95 - 1 + 8, 97 - 1 + 8, 104 + 8 - 1).map(nums::get).sum();
        int num8 = IntStream.of(15 - 1, 16 - 1, 20 - 1, 21 - 1, 25 - 1, 34 - 1, 37 - 1, 48 - 1, 57 - 1 + 8, 81 - 1 + 8, 97 - 1 + 8, 106 + 8 - 1).map(nums::get).sum();
        String desc1 = "正常", desc2 = "正常", desc3 = "正常", desc4 = "正常", desc5 = "正常", desc6 = "正常", desc7 = "正常", desc8 = "正常";
        if (num1 > 11) desc1 = "异常";
        if (num2 > 10) desc2 = "异常";
        if (num3 > 9) desc3 = "异常";
        if (num4 > 8) desc4 = "异常";
        if (num5 > 4) desc5 = "异常";
        if (num6 > 15) desc6 = "异常";
        if (num7 > 3) desc7 = "异常";
        if (num8 > 6) desc8 = "异常";

        resList.add(new ThirdVO("焦虑强迫", num1, desc1));
        resList.add(new ThirdVO("躯体诉述", num2, desc2));
        resList.add(new ThirdVO("分裂样", num3, desc3));
        resList.add(new ThirdVO("抑郁退缩", num4, desc4));
        resList.add(new ThirdVO("不成熟", num5, desc5));
        resList.add(new ThirdVO("违纪", num6, desc6));
        resList.add(new ThirdVO("攻击性", num7, desc7));
        resList.add(new ThirdVO("残忍", num8, desc8));
    }

    private void fun6_11_2(List<Integer> nums, List<ThirdVO> resList) {
        int num1 = IntStream.of(11 - 1, 12 - 1, 30 - 1, 31 - 1, 32 - 1, 33 - 1, 34 - 1, 35 - 1, 38 - 1, 45 - 1, 50 - 1, 52 - 1, 71 - 1 + 8, 75 - 1 + 8, 88 - 1 + 8, 103 - 1 + 8, 111 - 1 + 8, 112 + 8 - 1).map(nums::get).sum();
        int num2 = IntStream.of(13 - 1, 42 - 1, 65 - 1 + 8, 69 - 1 + 8, 75 - 1 + 8, 80 - 1 + 8, 87 - 1 + 8, 88 - 1 + 8, 102 - 1 + 8, 103 - 1 + 8, 111 + 8 - 1).map(nums::get).sum();
        int num3 = IntStream.of(2 - 1, 4 - 1, 7 - 1, 51 - 1, 54 - 1, 56 + 1 - 1, 56 + 2 - 1, 56 + 3 - 1, 56 + 4 - 1, 56 + 5 - 1, 56 + 6 - 1, 56 + 7 - 1, 77 - 1 + 8, 92 + 8 - 1).map(nums::get).sum();
        int num4 = IntStream.of(9 - 1, 18 - 1, 40 - 1, 66 - 1 + 8, 67 - 1 + 8, 70 - 1 + 8, 76 - 1 + 8, 84 - 1 + 8, 85 - 1 + 8, 91 - 1 + 8, 100 + 8 - 1).map(nums::get).sum();
        int num5 = IntStream.of(0, 8 - 1, 10 - 1, 13 - 1, 17 - 1, 23 - 1, 38 - 1, 41 - 1, 48 - 1, 61 - 1 + 8, 62 - 1 + 8, 64 - 1 + 8, 79 - 1 + 8, 80 + 8 - 1).map(nums::get).sum();
        int num6 = IntStream.of(52 - 1, 60 - 1 + 8, 63 - 1 + 8, 73 - 1 + 8, 93 - 1 + 8, 96 + 8 - 1).map(nums::get).sum();
        int num7 = IntStream.of(39 - 1, 43 - 1, 67 - 1 + 8, 81 - 1 + 8, 82 - 1 + 8, 90 + 8 - 1).map(nums::get).sum();
        int num8 = IntStream.of(3 - 1, 7 - 1, 14 - 1, 16 - 1, 19 - 1, 21 - 1, 22 - 1, 23 - 1, 25 - 1, 27, 33 - 1, 37 - 1, 41, 48 - 1, 68 - 1 + 8, 74 + 8 - 1, 86 - 1 + 8, 87 - 1 + 8, 88 - 1 + 8, 93 - 1 + 8, 94 - 1 + 8, 95 - 1 + 8, 97 - 1 + 8, 104 - 1 + 8, 109 + 8 - 1).map(nums::get).sum();
        int num9 = IntStream.of(5 - 1, 15 - 1, 16 - 1, 20 - 1, 21 - 1, 37 - 1, 57 + 8 - 1).map(nums::get).sum();
        String desc1 = "正常", desc2 = "正常", desc3 = "正常", desc4 = "正常", desc5 = "正常", desc6 = "正常", desc7 = "正常", desc8 = "正常", desc9 = "正常";
        if (num1 > 14) desc1 = "异常";
        if (num2 > 9) desc2 = "异常";
        if (num3 > 9) desc3 = "异常";
        if (num4 > 4) desc4 = "异常";
        if (num5 > 11) desc5 = "异常";
        if (num6 > 4) desc6 = "异常";
        if (num7 > 3) desc7 = "异常";
        if (num8 > 19) desc8 = "异常";
        if (num9 > 4) desc9 = "异常";

        resList.add(new ThirdVO("抑郁", num1, desc1));
        resList.add(new ThirdVO("社交退缩", num2, desc2));
        resList.add(new ThirdVO("躯体诉述", num3, desc3));
        resList.add(new ThirdVO("分裂强迫", num4, desc4));
        resList.add(new ThirdVO("多动", num5, desc5));
        resList.add(new ThirdVO("性问题", num6, desc6));
        resList.add(new ThirdVO("违纪", num7, desc7));
        resList.add(new ThirdVO("攻击性", num8, desc8));
        resList.add(new ThirdVO("残忍", num9, desc9));
    }

    private void fun4_5_2(List<Integer> nums, List<ThirdVO> resList) {
        int num1 = IntStream.of(11 - 1, 14 - 1, 24 - 1, 51 - 1, 54 - 1, 56 + 1 - 1, 56 + 2 - 1, 56 + 3 - 1, 56 + 7 - 1, 56 + 5 - 1, 56 + 6 - 1, 68 + 8 - 1, 71 - 1 + 8, 75 - 1 + 8, 77 - 1 + 8, 92 - 1 + 8, 102 + 8 - 1).map(nums::get).sum();
        int num2 = IntStream.of(9 - 1, 25 - 1, 31 - 1, 32 - 1, 33 - 1, 34 - 1, 35 - 1, 38 - 1, 48 - 1, 50 - 1, 52 - 1, 71 - 1 + 8, 87 - 1 + 8, 88 - 1 + 8, 103 - 1 + 8, 112 + 8 - 1).map(nums::get).sum();
        int num3 = IntStream.of(9 - 1, 11 - 1, 14 - 1, 19 - 1, 29 - 1, 34 - 1, 40 - 1, 45 - 1, 50 - 1, 70 - 1 + 8, 76 - 1 + 8, 84 - 1 + 8, 100 - 1 + 8, 109 + 8 - 1).map(nums::get).sum();
        int num4 = IntStream.of(0, 8 - 1, 13 - 1, 17 - 1, 18 - 1, 36 - 1, 46 - 1, 62 - 1 + 8, 65 - 1 + 8, 66 - 1 + 8, 69 - 1 + 8, 79 - 1 + 8, 80 - 1 + 8, 102 - 1 + 8, 111 + 8 - 1).map(nums::get).sum();
        int num5 = IntStream.of(5 - 1, 38 - 1, 47 - 1, 48 - 1, 53 - 1, 55 - 1, 85 - 1 + 8, 94 + 8 - 1).map(nums::get).sum();
        int num6 = IntStream.of(3 - 1, 15 - 1, 16 - 1, 20 - 1, 21 - 1, 22 - 1, 25 - 1, 26 - 1, 37 - 1, 41 - 1, 43 - 1, 48 - 1, 57 - 1 + 8, 67 - 1 + 8, 68 - 1 + 8, 86 - 1 + 8, 87 - 1 + 8, 90 - 1 + 8, 94 - 1 + 8, 95 - 1 + 8, 97 - 1 + 8, 104 + 8 - 1).map(nums::get).sum();
        int num7 = IntStream.of(5 - 1, 39 - 1, 59 - 1 + 8, 60 - 1 + 8, 73 - 1 + 8, 96 - 1 + 8, 110 + 8 - 1).map(nums::get).sum();
        int num8 = IntStream.of(8 - 1, 10 - 1, 23 - 1, 41 - 1, 61 - 1 + 8, 74 - 1 + 8, 93 - 1 + 8, 104 + 8 - 1).map(nums::get).sum();
        String desc1 = "正常", desc2 = "正常", desc3 = "正常", desc4 = "正常", desc5 = "正常", desc6 = "正常", desc7 = "正常", desc8 = "正常";
        if (num1 > 11) desc1 = "异常";
        if (num2 > 10) desc2 = "异常";
        if (num3 > 9) desc3 = "异常";
        if (num4 > 8) desc4 = "异常";
        if (num5 > 4) desc5 = "异常";
        if (num6 > 15) desc6 = "异常";
        if (num7 > 3) desc7 = "异常";
        if (num8 > 6) desc8 = "异常";

        resList.add(new ThirdVO("躯体诉述", num1, desc1));
        resList.add(new ThirdVO("忧郁", num2, desc2));
        resList.add(new ThirdVO("分裂样", num3, desc3));
        resList.add(new ThirdVO("社交退缩", num4, desc4));
        resList.add(new ThirdVO("肥胖", num5, desc5));
        resList.add(new ThirdVO("攻击性", num6, desc6));
        resList.add(new ThirdVO("性问题", num7, desc7));
        resList.add(new ThirdVO("多动", num8, desc8));
    }

    private void fun12_16_1(List<Integer> nums, List<ThirdVO> resList) {
        int num1 = IntStream.of(36 - 1, 49 - 1, 50 - 1, 51 - 1, 54 - 1, 56 + 1 - 1, 56 + 2 - 1, 56 + 3 - 1, 56 + 4 - 1, 56 + 5 - 1, 56 + 6 - 1, 56 + 7 - 1, 80 - 1 + 8, 102 - 1 + 8, 112 + 8 - 1).map(nums::get).sum();
        int num2 = IntStream.of(5 - 1, 11 - 1, 30 - 1, 31 - 1, 32 - 1, 40 - 1, 51 - 1, 52 - 1, 99 - 1 + 8, 102 - 1 + 8).map(nums::get).sum();
        int num3 = IntStream.of(13 - 1, 42 - 1, 65 - 1 + 8, 69 - 1 + 8, 71 - 1 + 8, 75 - 1 + 8, 80 - 1 + 8, 86 - 1 + 8, 87 - 1 + 8, 88 - 1 + 8, 89 - 1 + 8, 102 - 1 + 8, 103 - 1 + 8, 111 - 1 + 8, 112 + 8 - 1).map(nums::get).sum();
        int num4 = IntStream.of(0, 11 - 1, 14 - 1, 19 - 1, 64 - 1 + 8, 108 - 1 + 8, 109 + 8 - 1).map(nums::get).sum();
        int num5 = IntStream.of(7 - 1, 9 - 1, 17 - 1, 31 - 1, 66 - 1 + 8, 83 - 1 + 8, 84 - 1 + 8, 85 - 1 + 8, 104 + 8 - 1).map(nums::get).sum();
        int num6 = IntStream.of(0, 12 - 1, 20 - 1, 21 - 1, 25 - 1, 33 - 1, 34 - 1, 35 - 1, 37 - 1, 38 - 1, 48 - 1, 62 - 1 + 8, 64 - 1 + 8, 111 + 8 - 1).map(nums::get).sum();
        int num7 = IntStream.of(20 - 1, 21 - 1, 23 - 1, 39 - 1, 43 - 1, 61 - 1 + 8, 67 - 1 + 8, 72 - 1 + 8, 81 - 1 + 8, 82 - 1 + 8, 101 - 1 + 8, 105 - 1 + 8, 106 + 8 - 1).map(nums::get).sum();
        int num8 = IntStream.of(3 - 1, 10 - 1, 16 - 1, 19 - 1, 22 - 1, 27 - 1, 34 - 1, 37 - 1, 41 - 1, 45 - 1, 57 - 1 + 8, 68 - 1 + 8, 86 - 1 + 8, 87 - 1 + 8, 88 - 1 + 8, 89 - 1, 90 - 1 + 8, 93 - 1 + 8, 94 - 1 + 8, 95 - 1 + 8, 97 - 1 + 8, 104 + 8 - 1).map(nums::get).sum();
        int num9 = IntStream.of(0, 8 - 1, 10 - 1, 23 - 1, 41 - 1, 44 - 1, 45 - 1, 61 - 1 + 8, 62 - 1 + 8, 74 + 8 - 1).map(nums::get).sum();
        String desc1 = "正常", desc2 = "正常", desc3 = "正常", desc4 = "正常", desc5 = "正常", desc6 = "正常", desc7 = "正常", desc8 = "正常", desc9 = "正常";
        if (num1 > 11) desc1 = "异常";
        if (num2 > 8) desc2 = "异常";
        if (num3 > 15) desc3 = "异常";
        if (num4 > 6) desc4 = "异常";
        if (num5 > 6) desc5 = "异常";
        if (num6 > 11) desc6 = "异常";
        if (num7 > 9) desc7 = "异常";
        if (num8 > 19) desc8 = "异常";
        if (num9 > 10) desc9 = "异常";

        resList.add(new ThirdVO("躯体诉述", num1, desc1));
        resList.add(new ThirdVO("分裂样", num2, desc2));
        resList.add(new ThirdVO("交往不良", num3, desc3));
        resList.add(new ThirdVO("不成熟", num4, desc4));
        resList.add(new ThirdVO("强迫性", num5, desc5));
        resList.add(new ThirdVO("敌意性", num6, desc6));
        resList.add(new ThirdVO("违纪", num7, desc7));
        resList.add(new ThirdVO("攻击性", num8, desc8));
        resList.add(new ThirdVO("多动性", num9, desc9));
    }

    private void fun6_11_1(List<Integer> nums, List<ThirdVO> resList) {
        int num1 = IntStream.of(11 - 1, 29 - 1, 30 - 1, 40 - 1, 47 - 1, 50 - 1, 59 - 1 + 8, 70 - 1 + 8, 75 - 1 + 8).map(nums::get).sum();
        int num2 = IntStream.of(12 - 1, 14 - 1, 18 - 1, 31 - 1, 32 - 1, 33 - 1, 34 - 1, 35 - 1, 45 - 1, 50 - 1, 52 - 1, 71 - 1 + 8, 88 - 1 + 8, 89 - 1 + 8, 91 - 1 + 8, 103 - 1 + 8, 112 - 1 + 8).map(nums::get).sum();
        int num3 = IntStream.of(13 - 1, 65 - 1 + 8, 69 - 1 + 8, 71 - 1 + 8, 75 - 1 + 8, 80 - 1 + 8, 86 - 1 + 8, 103 - 1 + 8).map(nums::get).sum();
        int num4 = IntStream.of(9 - 1, 13 - 1, 17 - 1, 46 - 1, 47 - 1, 50 - 1, 54 - 1, 66 - 1 + 8, 76 - 1 + 8, 80 - 1 + 8, 83 - 1 + 8, 84 - 1 + 8, 85 - 1 + 8, 92 - 1 + 8, 93 - 1 + 8, 100 - 1 + 8).map(nums::get).sum();
        int num5 = IntStream.of(49 - 1, 51 - 1, 54 - 1, 56 + 1 - 1, 56 + 2 - 1, 56 + 3 - 1, 56 + 6 - 1, 56 - 1 + 7, 77 - 1 + 8).map(nums::get).sum();
        int num6 = IntStream.of(25 - 1, 34 - 1, 38 - 1, 42 - 1, 48 - 1, 64 - 1, 102 - 1, 111 - 1 + 8).map(nums::get).sum();
        int num7 = IntStream.of(0, 8 - 1, 10 - 1, 13 - 1, 17 - 1, 20 - 1, 41 - 1, 61 - 1, 62 - 1, 64 - 1, 79 - 1 + 8).map(nums::get).sum();
        int num8 = IntStream.of(3 - 1, 7 - 1, 16 - 1, 19 - 1, 22 - 1, 23 - 1, 25 - 1, 27 - 1, 37 - 1, 43 - 1, 48 - 1, 57 - 1, 68 - 1, 74 - 1, 86 - 1, 87 - 1, 88 - 1, 90 - 1, 93 - 1, 94 - 1, 95 - 1, 97 - 1, 104 - 1 + 8).map(nums::get).sum();
        int num9 = IntStream.of(20 - 1, 21 - 1, 23 - 1, 39 - 1, 43 - 1, 67 - 1, 72 - 1, 81 - 1, 82 - 1, 90 - 1, 101 - 1, 106 - 1 + 8).map(nums::get).sum();
        String desc1 = "正常", desc2 = "正常", desc3 = "正常", desc4 = "正常", desc5 = "正常", desc6 = "正常", desc7 = "正常", desc8 = "正常", desc9 = "正常";
        if (num1 > 6) desc1 = "异常";
        if (num2 > 10) desc2 = "异常";
        if (num3 > 6) desc3 = "异常";
        if (num4 > 9) desc4 = "异常";
        if (num5 > 7) desc5 = "异常";
        if (num6 > 6) desc6 = "异常";
        if (num7 > 11) desc7 = "异常";
        if (num8 > 20) desc8 = "异常";
        if (num9 > 8) desc9 = "异常";

        resList.add(new ThirdVO("分裂样", num1, desc1));
        resList.add(new ThirdVO("抑郁", num2, desc2));
        resList.add(new ThirdVO("交往不良", num3, desc3));
        resList.add(new ThirdVO("躯体诉述", num4, desc4));
        resList.add(new ThirdVO("社交退缩", num5, desc5));
        resList.add(new ThirdVO("多动", num6, desc6));
        resList.add(new ThirdVO("攻击", num7, desc7));
        resList.add(new ThirdVO("攻击性", num8, desc8));
        resList.add(new ThirdVO("违纪", num9, desc9));
    }

    private void fun4_5_1(List<Integer> nums, List<ThirdVO> resList) {
        int num1 = IntStream.of(13 - 1, 17 - 1, 18 - 1, 42 - 1, 45 - 1, 46 - 1, 65 + 8 - 1, 75 + 8 - 1, 80 + 8 - 1, 87 + 8 - 1, 91 + 8 - 1, 111 + 8 - 1).map(nums::get).sum();
        int num2 = IntStream.of(9 - 1, 12 - 1, 14 - 1, 19 - 1, 27 - 1, 29 - 1, 31 - 1, 32 - 1, 33 - 1, 34 - 1, 35 - 1, 47 - 1, 50 - 1, 52 - 1, 71 + 8 - 1, 75 + 8 - 1, 83 + 8 - 1, 87 + 8 - 1, 88 + 8 - 1, 89 + 8 - 1, 96 + 8 - 1, 102 + 8 - 1, 103 + 8 - 1, 111 + 8 - 1, 112 + 8 - 1).map(nums::get).sum();
        int num3 = IntStream.of(0, 8 - 1, 11 - 1, 13 - 1, 14 - 1, 28 - 1, 36 - 1, 38 - 1, 62 + 8 - 1, 64 + 8 - 1, 77 + 8 - 1, 79 + 8 - 1).map(nums::get).sum();
        int num4 = IntStream.of(24 - 1, 56 + 1 - 1, 56 + 2 - 1, 56 + 3 - 1, 56 + 6 - 1).map(nums::get).sum();
        int num5 = IntStream.of(5 - 1, 31 - 1, 59 + 8 - 1, 61 + 8 - 1, 73 + 8 - 1, 110 + 8 - 1).map(nums::get).sum();
        int num6 = IntStream.of(18 - 1, 40 - 1, 53 - 1, 66 + 8 - 1, 67 + 8 - 1, 70 + 8 - 1, 76 + 8 - 1, 85 + 8 - 1, 100 + 8 - 1).map(nums::get).sum();
        int num7 = IntStream.of(3 - 1, 10 - 1, 15 - 1, 16 - 1, 19 - 1, 20 - 1, 21 - 1, 22 - 1, 25 - 1, 26 - 1, 27 - 1, 37 - 1, 41 - 1, 43 - 1, 45 - 1, 57 + 8 - 1, 68 + 8 - 1, 74 + 8 - 1, 86 + 8 - 1, 87 + 8 - 1, 94 + 8 - 1, 95 + 8 - 1, 97 + 8 - 1, 104 + 8 - 1, 108 + 8 - 1).map(nums::get).sum();
        int num8 = IntStream.of(34 - 1, 36 - 1, 37 - 1, 38 - 1, 39 - 1, 46 - 1, 72 + 8 - 1, 81 + 8 - 1, 82 + 8 - 1, 90 + 8 - 1, 91 + 8 - 1, 106 + 8 - 1).map(nums::get).sum();
        String desc1 = "正常", desc2 = "正常", desc3 = "正常", desc4 = "正常", desc5 = "正常", desc6 = "正常", desc7 = "正常", desc8 = "正常";
        if (num1 > 7) desc1 = "异常";
        if (num2 > 14) desc2 = "异常";
        if (num3 > 11) desc3 = "异常";
        if (num4 > 5) desc4 = "异常";
        if (num5 > 2) desc5 = "异常";
        if (num6 > 5) desc6 = "异常";
        if (num7 > 20) desc7 = "异常";
        if (num8 > 5) desc8 = "异常";

        resList.add(new ThirdVO("社交退缩", num1, desc1));
        resList.add(new ThirdVO("忧郁", num2, desc2));
        resList.add(new ThirdVO("不成熟", num3, desc3));
        resList.add(new ThirdVO("躯体诉述", num4, desc4));
        resList.add(new ThirdVO("性问题", num5, desc5));
        resList.add(new ThirdVO("分裂样", num6, desc6));
        resList.add(new ThirdVO("攻击", num7, desc7));
        resList.add(new ThirdVO("违纪", num8, desc8));
    }

    private static String judge(double num1) {
        if (num1 < 2) return "偏低";
        else if (num1 > 69) return "偏高";
        else return "正常";
    }

    private int sum7(ResDTO.SecondDTO second, int sum3) {
        //7_1
        if (second.getV7_1() == 2) {
            int avg = sum7_1_x(second, 0);
            sum3 += avg;
        }
        //7_2
        if (second.getV7_2() == 1) sum3++;
        //7_3
        if (second.getV7_3() == 1) sum3++;
        //7_4
        if (second.getV7_4() == 1) sum3++;

        return sum3;
    }

    private int sum7_1_x(ResDTO.SecondDTO second, int avg) {
        Integer num1 = second.getV7_1_1();
        Integer num2 = second.getV7_1_2();
        Integer num3 = second.getV7_1_3();
        Integer num4 = second.getV7_1_4();
        Integer num5 = second.getV7_1_5();
        Integer num6 = second.getV7_1_6();
        Integer num7 = second.getV7_1_7();


        if (num2 != null)  avg += (num2 - 1);
        if (num3 != null) avg += (num3 - 1);
        if (num4 != null) avg += (num4 - 1);
        if (num5 != null) avg += (num5 - 1);
        if (num6 != null) avg += (num6 - 1);
        if (num7 != null) avg += (num7 - 1);
        return avg / 7;
    }

    private int sum3And5And6(ResDTO.SecondDTO second, int sum2) {
        //3
        if (second.getV3_1() == 2) {
            List<String> v3_1hobbies = second.getV3_1hobbies();
            v3_1hobbies = v3_1hobbies.stream().filter(hobby -> !hobby.isEmpty()).collect(Collectors.toList());
            if (v3_1hobbies.size() == 2) sum2++;
            else if (v3_1hobbies.size() >= 3) sum2 += 2;
        }
        Integer v3_2 = second.getV3_2();
        if (v3_2 == 3) sum2++;
        else if (v3_2 == 4) sum2 += 2;
        //5
        Integer v5_1 = second.getV5_1();
        if (v5_1 == 3) sum2++;
        else if (v5_1 == 4) sum2 += 2;
        Integer v5_2 = second.getV5_2();
        if (v5_2 == 2) sum2++;
        else if (v5_2 == 3) sum2 += 2;
        //6
        int avg = 0;
        Integer v6_1_1 = second.getV6_1_1();
        if (v6_1_1 == 2) avg++;
        else if ((v6_1_1 == 3)) avg += 2;
        Integer v6_1_2 = second.getV6_1_2();
        if (v6_1_2 == 2) avg++;
        else if (v6_1_2 == 3) avg += 2;
        Integer v6_1_3 = second.getV6_1_3();
        if (v6_1_3 == 2) avg++;
        else if (v6_1_3 == 3) avg += 2;
        avg /= 3;
        sum2 += avg;
        Integer v6_1_4 = second.getV6_1_4();
        if (v6_1_4 == 2) sum2++;
        else if (v6_1_4 == 3) sum2 += 2;

        return sum2;
    }

    private int sum1And2And3And4(ResDTO.SecondDTO second, int sum1) {
        //1
        if (second.getV1_1() == 2) {
            List<String> v1_1hobbies = second.getV1_1hobbies();
            v1_1hobbies = v1_1hobbies.stream().filter(hobby -> !hobby.isEmpty()).collect(Collectors.toList());
            if (v1_1hobbies.size() == 2) sum1++;
            else if (v1_1hobbies.size() >= 3) sum1 += 2;
        }
        Integer v1_2 = second.getV1_2();
        if (v1_2 == 3) sum1++;
        else if (v1_2 == 4) sum1 += 2;
        Integer v1_3 = second.getV1_3();
        if (v1_3 == 3) sum1++;
        else if (v1_3 == 4) sum1 += 2;
        //2
        if (second.getV2_1() == 2) {
            List<String> v2_1hobbies = second.getV2_1hobbies();
            v2_1hobbies = v2_1hobbies.stream().filter(hobby -> !hobby.isEmpty()).collect(Collectors.toList());
            if (v2_1hobbies.size() == 2) sum1++;
            else if (v2_1hobbies.size() >= 3) sum1 += 2;
        }
        Integer v2_2 = second.getV2_2();
        if (v2_2 == 3) sum1++;
        else if (v2_2 == 4) sum1 += 2;
        Integer v2_3 = second.getV2_3();
        if (v2_2 == 3) sum1++;
        else if (v2_2 == 4) sum1 += 2;
        //3
        if (second.getV3_1() == 2) {
            List<String> v3_1hobbies = second.getV3_1hobbies();
            v3_1hobbies = v3_1hobbies.stream().filter(hobby -> !hobby.isEmpty()).collect(Collectors.toList());
            if (v3_1hobbies.size() == 2) sum1++;
            else if (v3_1hobbies.size() >= 3) sum1 += 2;
        }
        Integer v3_2 = second.getV3_2();
        if (v3_2 == 3) sum1++;
        else if (v3_2 == 4) sum1 += 2;
        //4
        if (second.getV4_1() == 2) {
            List<String> v4_1hobbies = second.getV4_1hobbies();
            v4_1hobbies = v4_1hobbies.stream().filter(hobby -> !hobby.isEmpty()).collect(Collectors.toList());
            if (v4_1hobbies.size() == 2) sum1++;
            else if (v4_1hobbies.size() >= 3) sum1 += 2;
        }
        Integer v4_2 = second.getV4_2();
        if (v4_2 == 3) sum1++;
        else if (v4_2 == 4) sum1 += 2;

        return sum1;
    }

//    public static double getNum(int lowBit, int highBit, int score, double lowScore, double highScore) {
//        double one = lowBit + (score - lowScore) / (highScore - lowScore) * (highBit - lowBit);
//        return new BigDecimal(one).setScale(2, RoundingMode.HALF_UP).doubleValue();
//    }
}
